package com.wl.application.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wl.application.fallback.Sentinel2Fallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author wl
 * @version 1.0
 * @date 2022/7/21
 * @description:
 */
@RestController
@RequestMapping("/sentinel2")
public class SentinelController {
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 线程流控
     */
    @GetMapping("/test1")
    public String serverText1() throws InterruptedException {
        Thread.sleep(3000);
        return "sentinel1:::"+System.currentTimeMillis();
    }
    @GetMapping("/test2")
    public String serverText2(){
        return "sentinelTest2:::"+System.currentTimeMillis();
    }
    @GetMapping("/test3")
    @SentinelResource(value = "myTest3",blockHandler = "Test3",
            fallback = "errorTest3",fallbackClass = Sentinel2Fallback.class )
    public String serverText3(Integer a){
        int b=10/a;
        return "sentinel3:::"+System.currentTimeMillis();
    }
    public String Test3(Integer a, BlockException b){
        return "服务被限流了"+a;
    }

    @GetMapping("/test4")
    @SentinelResource("myTest4")
    public String serverText4(Integer a,Integer b){
        return "sentinel4:::"+System.currentTimeMillis()+"a = " + a + ", b = " + b;
    }
    @GetMapping("/test5")
    public String serverText5(){
        return "sentinel5:::"+System.currentTimeMillis();
    }

}
